package com.apes.fn.server.workDesk.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.util.MapUtil;

import java.util.Arrays;
import java.util.Map;

/**
 * @program: apes-server
 * @description:
 * @author: czs
 * @create: 2023-06-15 11:20
 **/
public class SmartHelperUtils {
    private final static String FORM_ID_CAR_CONTROL =  "report/fn.control.obtainMainList";
    private final static String FORM_ID_SERVICE_PLAN_LIST =  "report/servicePlan.query";
    private final static String ACTION_LINE =  "link";
    private final static String QUERY_API =  "pub.query";
    private final static String ACTION_DRAWER =  "drawer";

    /**
     * 返回所需格式 key:{name:value}
     *
     * @param resultJson
     * @param nameLis
     * @return
     */
    public static JSONObject formatJson(JSONObject resultJson, String... nameLis) {
        JSONObject newResultJson = new JSONObject();
        if (nameLis.length % 2 != 0) {
            throw new RuntimeException("不正确的输入列");
        }
        for (int i = 0; i < nameLis.length; i++) {
            String name;
            String key;
            String value;
            if (i % 2 == 0) {
                key = nameLis[i];
                value = resultJson.getString(nameLis[i]);
                name = nameLis[i + 1];
                putJson(newResultJson, key, name, value);
            }
        }
        return newResultJson;
    }

    private static void putJson(JSONObject resultJson, String key, String name, Object value) {
        if (value==null)return;
        resultJson.put(key, MapUtil.mapper("name", name, "value", value+""));
    }

    public static void findJumpPageData(Map jsonObject){
        JSONArray data = (JSONArray) jsonObject.get("data");
        if (data==null||data.isEmpty())return;
        for (Object o : data) {
            JSONObject jsonObject1;
            if (o instanceof Map){
                jsonObject1 = new JSONObject((Map) o);
            }else {
                jsonObject1 = (JSONObject) o;
            }

            String id = jsonObject1.getString("ID");
            if (id==null)break;
            appendData(jsonObject1,jsonObject);
        }


    }

    /**
     * @param  response App响应
     * @return JSONArray
     * @author czs
     * @creed: 格式化 v1/store-data/statistical-data 接口数据
     * @date  2023年6月27日 11:41:07
     */
    public static JSONArray appStatisticalDataToJson(JSONObject response){
        JSONArray data = response.getJSONArray("data");
        JSONArray resultArray = new JSONArray();
        for (Object o : data) {
            JSONObject jsonObject = (JSONObject) o;
            String type = jsonObject.getString("type");
            String value = jsonObject.getString("value");
            if ("0".equals(value))continue;
            resultArray.add(MapUtil.mapper("ID",type,"NAME",appStatisticalDataGetTypeName(type),"COUNT",value,
                    "COLOR","black","WeightValue",appStatisticalDataGetWeightValue(type)));
        }
        return resultArray;
    }
    private static String appStatisticalDataGetTypeName(String type){
        switch (type){
            case "newMember":
                return "新增会员";
            case "reservationTimeout":
                return "预约超时";
            case "readyToStore":
                return "准备到店";
            case "unReservation":
                return "未预约";
                default:return type;
        }
    }

    /**
     *
     * @param type
     * @return
     */
    private static Integer appStatisticalDataGetWeightValue(String type){
        switch (type){
            case "newMember":
            case "reservationTimeout":
                return 0;
            case "readyToStore":
                return 10;
            case "unReservation":
                return 20;
                default:return 30;
        }
    }

    /**
     * 增加跳转条件
     * @param target
     * @param param
     */
    private static void appendData(JSONObject target, Map param) {
        String id = target.getString("ID");
        switch (id){
            case "ZK000754"://在店车辆
            case "ZK000755"://内协车辆
            case "ZK000753"://待交车
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId", "custom/vehicleControl/VehicleControlComponent",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "formId", "custom/vehicleControl/VehicleControlComponent",
                                "wfFlag",true,
                                "id",id,
                                "api",QUERY_API)
                ));
                break;
            case "ZK000752"://待开单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_DRAWER,
                        "formId","fn.workDeskAssistantConfig.outScheme",
                        "api","fn.workDeskConfig.assistantDrawer",
                        "condition",MapUtil.mapper(
                                "queryId","workDesk.workDeskByDKD")
                ));
                break;
            case "XJTH"://询价退回
                target.putAll(MapUtil.mapper(
                        "action",ACTION_DRAWER,
                        "formId","fn.workDeskAssistantConfig.enquiry",
                        "api","fn.workDeskConfig.assistantDrawer",
                        "condition",MapUtil.mapper(
                                "queryId","workDesk.workDeskByXJ",
                                "query_type","XJTH"
                        )
                ));
                break;
            case "XJZ"://询价中
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/sp.enquiry.result",
                        "condition",MapUtil.mapper(
                                "STATUS","ing",
                                "CREATE_DATE", new JSONArray(),
                                "formId","report/sp.enquiry.result",
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;
            case "Service"://服务方案
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId", FORM_ID_SERVICE_PLAN_LIST,
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "STATE", Arrays.asList("qualityTest","confirm"),
                                "formId", FORM_ID_SERVICE_PLAN_LIST,
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "qualityTest"://待收款
                target.putAll(MapUtil.mapper(
                        "action",ACTION_DRAWER,
                        "formId","fn.workDeskAssistantConfig.awaitBillingOrNotBillingOutCar",
                        "api","fn.workDeskConfig.assistantDrawer",
                        "condition",MapUtil.mapper(
                                "queryId","workDesk.workDeskByDSK",
                                "CREATE_DATE_LT", target.getDate("LTCREATEDATESQL"),
                                "CREATE_DATE_GT",target.getDate("GTCREATEDATE")
                                )
                ));
                break;
            case "waitQualityTest"://待质检
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId", FORM_ID_SERVICE_PLAN_LIST,
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "STATE", Arrays.asList("awaitQuality"),
                                "formId", FORM_ID_SERVICE_PLAN_LIST,
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;
            case "wait"://待施工
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId", "report/fn.servicePlanItem.query",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "TICKETS_STATE", Arrays.asList("create"),
                                "WORK_STATE", Arrays.asList("wait"),
                                "formId", "report/fn.servicePlanItem.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;
            case "working"://施工中
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId", "report/fn.servicePlanItem.query",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "TICKETS_STATE", Arrays.asList("create"),
                                "WORK_STATE", Arrays.asList("working"),
                                "formId", "report/fn.servicePlanItem.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;
            case "pause"://施工中
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId", "report/fn.servicePlanItem.query",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "TICKETS_STATE", Arrays.asList("create"),
                                "WORK_STATE", Arrays.asList("pause"),
                                "formId", "report/fn.servicePlanItem.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;
            case "LeaveWithoutPaying"://未结算交车
                target.putAll(MapUtil.mapper(
                        "action",ACTION_DRAWER,
                        "formId","fn.workDeskAssistantConfig.awaitBillingOrNotBillingOutCar",
                        "api","fn.workDeskConfig.assistantDrawer",
                        "condition",MapUtil.mapper(
                                "queryId","workDesk.workDeskByWJSD",
                                "CREATE_DATE_LT", target.getDate("LTCREATEDATESQL"),
                                "CREATE_DATE_GT",target.getDate("GTCREATEDATE")
                        )
                ));
                break;
            case "toPriceAudit"://价格审核-审核中
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId",FORM_ID_SERVICE_PLAN_LIST,
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("toPriceAudit"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "formId",FORM_ID_SERVICE_PLAN_LIST,
                                "OAInterface",false,
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "backPriceAudit"://价格审核-退回
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId",FORM_ID_SERVICE_PLAN_LIST,
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("backPriceAudit"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "formId",FORM_ID_SERVICE_PLAN_LIST,
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "waitReceive"://待领用
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/fn.servicePlanItem.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("confirm"),
                                "ADOPTION_STATE", Arrays.asList("not","part"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "formId","report/fn.servicePlanItem.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "ServiceNotConfirm"://方案未确认
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId",FORM_ID_SERVICE_PLAN_LIST,
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("submission"),
                                "CREATE_DATE", Arrays.asList(target.get("LTCREATEDATESQL"),target.get("GTCREATEDATE")),
                                "formId",FORM_ID_SERVICE_PLAN_LIST,
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "CarAccident"://车险报案单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/carAccident.query",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "STATE", Arrays.asList("create","store","backStage","inStore"),
                                "CREATE_DATE", new JSONArray(),
                                "formId","report/carAccident.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "LossQuotation"://定损报价单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/lossQuotation.query",
                        "condition",MapUtil.mapper("FIXED_LOSS_DEPT",param.get("DEPT_ID"),
                                "STATE", Arrays.asList("create","confirmPlan","techAudit","toBusinessAudit","inquiryFinish","suspend"),
                                "CREATE_DATE", new JSONArray(),
                                "formId","report/lossQuotation.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "deliver"://调拨-待发货-未发货
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/pick.query",
                        "condition",MapUtil.mapper("_showType","1",
                                "OPERATION", "deliver",
                                "STATE_ID", Arrays.asList("confirmed"),
                                "CARGO_STATE", "0",
                                "formId","report/pick.query",
                                "wfFlag",true,
                                "changeShowType", MapUtil.mapper("id","1","name","交货单明细","api","pick.query"),
                                "api",QUERY_API)
                        ));
                break;
            case "receipt"://调拨-待收货
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/pick.query",
                        "condition",MapUtil.mapper("_showType","1",
                                "OPERATION", "receipt",
                                "STATE_ID", Arrays.asList("confirmed"),
                                "CARGO_STATE", "1",
                                "formId","report/pick.query",
                                "wfFlag",true,
                                "changeShowType", MapUtil.mapper("id","1","name","交货单明细","api","pick.query"),
                                "api",QUERY_API)
                        ));
                break;
            case "moneyIncomeNotApply"://资金收入单-未审核
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/moneyIncome.query",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "STATE", "sent",
                                "CREATE_DATE", new JSONArray(),
                                "formId","report/moneyIncome.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "RetailReturn.orderType.A"://零售退货-有单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/retail.returnWithOrder.query",
                        "condition",MapUtil.mapper("DEPT_ID",Arrays.asList(param.get("DEPT_ID")),
                                "DEPT_ID_name",Arrays.asList(param.get("DEPT_NAME")),
                                "STATE", Arrays.asList("sent","confirmed"),
                                "CREATE_DATE", new JSONArray(),
                                "formId","report/retail.returnWithOrder.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "RetailReturn.orderType.B"://零售退货-无单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/retail.returnNoOrder.query",
                        "condition",MapUtil.mapper("DEPT_ID",Arrays.asList(param.get("DEPT_ID")),
                                "DEPT_ID_name",Arrays.asList(param.get("DEPT_NAME")),
                                "STATE", Arrays.asList("sent","confirmed"),
                                "CREATE_DATE", new JSONArray(),
                                "formId","report/retail.returnNoOrder.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "merchantSettlementV1"://结算-赋能商户结算对账单1.0
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/fn.merchant.settle.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("financialAudit","sent","confirmation"),
                                "formId","report/fn.merchant.settle.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "merchantSettlementV2"://结算-赋能商户结算对账单2.0
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/merchant.settle.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("financialAudit","sent","confirmation"),
                                "formId","report/merchant.settle.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "ClientSettle"://结算-客户结算单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/sal.settle.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("sent","approve"),
                                "formId","report/sal.settle.query",
                                "CREATE_DATE", new JSONArray(),
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "invoiceApply"://结算-发票申请单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/fn.invoiceApply.query",
                        "condition",MapUtil.mapper(
                                "DEPT_ID",param.get("DEPT_ID"),
                                "STATE", "sent",
                                "_showType", "1",
                                "formId","report/fn.invoiceApply.query",
                                "changeShowType", MapUtil.mapper("id","1","name","平台服务费","api","invoiceApply.platformService_query"),
                                "CREATE_DATE", new JSONArray(),
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "invoiceApplyByDirect"://结算-发票申请单-直营
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/direct.invoiceApply.query",
                        "condition",MapUtil.mapper("DEPT_ID",param.get("DEPT_ID"),
                                "DEPT_ID_name",param.get("DEPT_NAME"),
                                "STATE", "sent",
                                "formId","report/direct.invoiceApply.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "applyJobOrder": //查询人员账号-入职申请单

                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/hr.applyJobOrder.query",
                        "condition",MapUtil.mapper("DEPT_ID",Arrays.asList(param.get("DEPT_ID")),
                                "STATE", Arrays.asList("create","firstTrial","return"),
                                "formId","report/hr.applyJobOrder.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "transferApplication"://调动申请单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/hr.transferApplication.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("create"),
                                "formId","report/hr.transferApplication.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "supportApplication"://支援申请单
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/hr.support.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("create"),
                                "formId","report/hr.support.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "entryInformation"://人员资料补充
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/hr.entryInfo.query",
                        "condition",MapUtil.mapper(
                                "STATE", Arrays.asList("create"),
                                "formId","report/hr.entryInfo.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "requireUnAudit"://要货跟踪-未审核
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/productRequire.query",
                        "condition",MapUtil.mapper(
                                "ARRIVE_STATE", Arrays.asList(MapUtil.mapper("name","未处理","checked",false,"id",1)),
                                "formId","report/productRequire.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "requireUnAssigned"://要货跟踪-未配货
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/productRequire.query",
                        "condition",MapUtil.mapper(
                                "ARRIVE_STATE", Arrays.asList(MapUtil.mapper("name","未配货","checked",false,"id",2)),
                                "formId","report/productRequire.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "requireUnDelivery"://要货跟踪-未到货
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/productRequire.query",
                        "condition",MapUtil.mapper(
                                "ARRIVE_STATE", Arrays.asList(MapUtil.mapper("name","未到货","checked",false,"id",3)),
                                "formId","report/productRequire.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                        ));
                break;
            case "051"://库存预警
                target.putAll(MapUtil.mapper(
                        "action",ACTION_DRAWER,
                        "formId","fn.workDesk.stock",
                        "api","fn.workDeskConfig.stockWarring",
                        "condition",MapUtil.mapper(
                                "queryId","fn.workDeskUseCount.countUse"
                        )
                ));
                break;
            case "052"://滞销商品
                target.putAll(MapUtil.mapper(
                        "action",ACTION_DRAWER,
                        "formId","fn.workDesk.WaitStock",
                        "api","fn.workDeskConfig.unsalableStock",
                        "condition",MapUtil.mapper(
                                "queryId","fn.workDeskUseCount.countUse"
                        )
                ));
                break;
            case "xsInvoice"://挂账应收
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/acc.invoice.query",
                        "condition",MapUtil.mapper(
                                "DEPT_ID",param.get("DEPT_ID"),"DEPT_ID_name",param.get("DEPT_NAME"),
                                "SETTLE_QTY_T","n",
                                "formId","report/acc.invoice.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;
            case "WaitRevisit"://待售后回访
                target.putAll(MapUtil.mapper(
                        "action",ACTION_LINE,
                        "formId","report/fn.returnVisit.query",
                        "condition",MapUtil.mapper(
                                "STATE",Arrays.asList("create","again"),
                                "formId","report/fn.returnVisit.query",
                                "wfFlag",true,
                                "api",QUERY_API)
                ));
                break;

        }
    }
}
